import React from 'react'
import { ImagePicker } from 'antd-mobile';
import axios from 'axios'

class ImagePickerExample extends React.Component {
  state = {
    files: [],
  };
  onChange = async (files, type, index) => {
    console.log(files, type, index);

    if (type === 'add') {
      var form = new FormData();
      form.append("file", files[0].file);
      form.append("bucket", "dxtoss");
      console.log(form, 'form');
      try {
        const res = await axios({
          data: form,
          url: "/wmjapi/upload",
          method: "post"
        })
        this.props.imageChange(res.data.url)
      } catch (err) {
        throw err
      }
    }

    this.setState({
      files
    });


  };
  onAddImageClick = (e) => {
    e.preventDefault();
  };
  onTabChange = (key) => {
    console.log(key);
  };
  render() {
    const { files } = this.state;
    return (
      <div>
        <ImagePicker
          onChange={this.onChange}
          selectable={files.length < 1}
        />
      </div>
    );
  }
}

export default ImagePickerExample